<template>
  <div class="home">
    <div class="home-header">
      <a href class="home-header-logo">
        <img :src="logo" alt />
        <span>我爱报销</span>
      </a>
      <van-icon name="setting-o" class="icon-set" @click="goSet" />
    </div>
    <div class="home-userInfo">
      <van-image class="avatar" round fit="cover" :src="photo" @click="openEditAvatar" />
      <span class="name">{{$store.state.user.nickname}}</span>
    </div>
    <div class="home-content">
      <van-tabs v-model="active" @change="tabsChange">
        <van-tab title="我的订单"></van-tab>
        <van-tab title="已完成订单"></van-tab>
      </van-tabs>
      <van-list
        v-model="listLoading"
        :finished="finished"
        :round="true"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div class="item" v-for="(item,index) in dataList" :key="index">
          <van-cell :title="item.order_number" :value="item.status" @click="goDetails(item)">
            <template #label>
              <span class="mr10">{{item.service_project | project}}</span>
              <span>{{item.service_type | type}}</span>
            </template>
            <template #default>
              <span :class="item.status | statusColor">{{item.status | status}}</span>
              <p v-if="active == 0">
                <span v-if="item.status == 5" class="font_danger">有效时间：{{item.invalid_cn}}分钟</span>
                <span v-else>{{item.created_at}}</span>
              </p>
              <p v-else>
                <span>{{item.updated_at}}</span>
              </p>
            </template>
          </van-cell>
        </div>
      </van-list>
    </div>

    <draggingBtn @onFloatBtnClicked="goSumbit">
      <template slot="icon">
        <van-icon name="plus" />
      </template>
    </draggingBtn>
    <editAvatar ref="editAvatar" />
  </div>
</template>

<script>
import draggingBtn from "@cb/dragging-btn.vue";
import editAvatar from "@cm/edit-avatar.vue";
import { getOrder } from "@a/order.js";
import { Dialog, Toast } from "vant";
export default {
  components: {
    draggingBtn,
    editAvatar,
  },
  data() {
    return {
      logo: require("@/assets/img/logo.png"),
      active: 0,
      dataList: [],
      listLoading: false,
      finished: false,
      page: 1,
      status: [1, 2, 3, 5, 6, 7],
      baseUrls: process.env.VUE_APP_BASE_URL,
      originUrl: process.env.VUE_APP_ORIGIN_URL,
    };
  },
  computed: {
    photo() {
      return (
        
        this.$store.state.user.photo
      );
    },
  },
  deactivated() {
    this.$destroy(true); //销毁组件
  },
  created() {
    // 判断是否有oa账号 给提示
    let isOATipOver = sessionStorage.getItem("isOATipOver");
    let first = this.$store.state.user.first;

    if (first == 1 && isOATipOver == 'false') {
      Dialog.confirm({
        title: "提示",
        message: "您还没有绑定OA账号，这会影响到您的订单进度，请尽快完善资料。",
        confirmButtonText: "立即绑定",
        cancelButtonText: "暂不绑定",
      })
        .then(() => {
          this.$router.push("/user/set/setOAAccount");
          sessionStorage.setItem("isOATipOver", true);
        })
        .catch(() => {
          sessionStorage.setItem("isOATipOver", true);
          Dialog.alert({
            message: `您可以随时点击右上角的“齿轮”图标到 <a href='${this.originUrl}/user/set' class='btn_text'>个人中心<a/> 中的 <a href='${this.originUrl}/user/set/setOAAccount' class='btn_text' >修改OA账号<a/> 界面修改。`,
            allowHtml: true
          }).then(() => {
            
          });
        });
    }
  },
  methods: {
    goSumbit() {
      this.$router.push("/user/apply");
    },
    goSet() {
      this.$router.push("/user/set");
    },
    openEditAvatar() {
      this.$refs["editAvatar"].show();
    },
    onLoad() {
      this.listLoading = true;
      this.getData();
      this.page++;
    },
    getData(args) {
      let params = {
        page: this.page,
        status: this.status,
      };

      if (args) {
        params = {
          ...args,
          ...params,
        };
        this.page = 1;
        params.page = this.page;
      }
      params = {
        ...this.params,
        ...params,
      };

      getOrder(params).then((res) => {
        if (res.code === 200) {
          this.listLoading = false;
          this.dataList = this.dataList.concat(res.data);

          // 如果加载完毕，显示没有更多了
          if (res.data.length === 0) {
            this.finished = true;
          }
        } else {
          this.listLoading = false;
          this.finished = true;
        }
      });
    },
    goDetails(item) {
      this.$router.push({
        path: `/user/home/details/${item.id}`,
        params: {
          id: item.id,
        },
      });
    },
    tabsChange(name) {
      console.log(name);
      switch (name) {
        case 0:
          this.dataList = [];
          this.status = [1, 2, 3, 5, 6, 7];
          this.page = 1;
          this.getData();
          return;
        case 1:
          this.dataList = [];
          this.status = [4];
          this.page = 1;
          this.getData();
          return;
      }
    },
  },
  filters: {},
};
</script>

<style lang="less">
.home {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  margin: auto;
  padding-top: 60px;

  &-header {
    height: 60px;
    width: 100%;
    font-size: 16px;
    // box-shadow: 0px 5px 10px 0px rgba(211, 211, 211, 0.5);
    padding: 0 10px 0 15px;
    box-sizing: border-box;
    position: fixed;
    background: #fff;
    top: 0;
    z-index: 2020;

    &-logo {
      display: inline-block;
      text-decoration: none;
      height: 100%;
      line-height: 60px;

      > img {
        width: 60px;
        height: 30px;
        object-fit: contain;
        vertical-align: middle;
        margin-right: 8px;
      }

      > span {
        font-size: 16px;
        font-weight: 800;
        color: rgba(53, 53, 53, 1);
      }
    }

    .icon-set {
      font-size: 20px;
      vertical-align: middle;
      color: #353535;
      position: absolute;
      right: 10px;
      top: 50%;
      margin-top: -9px;
    }
  }

  &-userInfo {
    height: 65px;
    width: 100%;
    background: linear-gradient(138deg, #425bff 0%, #0055eb 100%);
    position: relative;
    display: flex;
    flex-direction: row;
    align-items: center;
    box-sizing: border-box;

    .avatar {
      width: 50px;
      height: 50px;
      vertical-align: middle;
      margin: 0 10px 0 20px;
    }

    .name {
      font-size: 16px;
      font-weight: 700;
      color: #fff;
    }

    .icon-edit {
      width: 16px;
      height: 16px;
      background: url("../../../assets/img/icons/edit.png") no-repeat;
      background-size: 100%;
      position: absolute;
      bottom: 0;
      left: 45px;
    }
  }

  &-content {
    .van-tabs {
      .van-tabs__wrap {
        background: #f4f4f4;
      }

      .van-tabs__line {
        background: #1256f0;
      }
    }
  }
}
</style>
